//
//  ZYCLectureRecordHandleExceptionBottomView.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/23.
//

#import "ZYCLectureRecordHandleExceptionBottomView.h"


#define MaxCount 40

#define ZYCContentPlaceholder @"请输入您的异议~"
#define ZYCContentPlaceholderColor [UIColor colorWithHexString:@"#C3C3C3" alpha:1.0]

@interface ZYCLectureRecordHandleExceptionBottomView ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textContentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *line;
@end
@implementation ZYCLectureRecordHandleExceptionBottomView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"FFFFFF");
        [self addSubview:self.titleLabel];
        [self addSubview:self.line];
        [self addSubview:self.textContentView];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(14.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14.0f);
            make.right.equalTo(self).offset(-14.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(5.0f);
            make.height.mas_equalTo(0.5f);
        }];
        [self.textContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.line.mas_bottom).offset(16.0f);
            make.height.mas_equalTo(150.0f);
            make.left.right.bottom.equalTo(self);
        }];
    }
    return self;
}


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithString:@"异议:" Font:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#2B302D") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}


- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _line;
}
- (UITextView *)textContentView {
    if (!_textContentView) {
        _textContentView = [[UITextView alloc] init];
        _textContentView.delegate = self;
        _textContentView.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        _textContentView.text = ZYCContentPlaceholder;
        _textContentView.textColor = ZYCContentPlaceholderColor;
    }
    return _textContentView;
}

#pragma mark - UITextViewDelegate
/// 用此代理方法限制字数，因为在iOS9中选择联想词不会走(textView:shouldChangeTextInRange:replacementText:)这个方法
- (void)textViewDidChange:(UITextView *)textView {
    // textView.markedTextRange == nil 说明当前不是正在打字的高亮状态
    if (textView.markedTextRange == nil) {
        if (textView.text.length > MaxCount) {
            textView.text = [textView.text substringToIndex:MaxCount];
        }
    }
}

-(void) textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:ZYCContentPlaceholder]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        textView.text = ZYCContentPlaceholder;
        textView.textColor = ZYCContentPlaceholderColor;
    }
}

- (NSString *)exceptContent{
    if ([self.textContentView.text isEqualToString:ZYCContentPlaceholder]) {
        return nil;
    }
    return self.textContentView.text;
}

@end
